﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using Microsoft.Win32;
using System.Security;
using System.Net;
using System.Globalization;
using System.IO;

namespace ImPM.Application
{
    public sealed class ImPMEventLog : IDisposable
    {
        // Fields
        private EventLog eventLog;

        // Methods
        public ImPMEventLog()
        {
            this.eventLog = null;
        }

        public ImPMEventLog(EventLog eventLog)
        {
            eventLog.Log = "Application";
            this.eventLog = eventLog;
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.eventLog.Dispose();
            }
        }

        [Conditional("POST_HTTP")]
        private static void PostHttpLinkRequest(string eventSource, EventLogEntryType eventType, long eventId)
        {
            bool flag = false;
            if (flag)
            {
                string str = null;
                int num2 = 0;
                switch (eventType)
                {
                    case EventLogEntryType.Error:
                        str = "Error";
                        num2 = 1;
                        break;

                    case EventLogEntryType.Warning:
                        str = "Warning";
                        num2 = 2;
                        break;

                    case EventLogEntryType.Information:
                        str = "Information";
                        num2 = 4;
                        break;

                    case EventLogEntryType.SuccessAudit:
                        str = "Information";
                        num2 = 0;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("eventType");
                }
                string fileName = null;
                try
                {
                    RegistryKey key = Registry.LocalMachine.OpenSubKey(string.Format(CultureInfo.InvariantCulture, @"SYSTEM\CurrentControlSet\Services\Eventlog\Application\{0}", new object[] { eventSource }), false);
                    if (key == null)
                    {
                        return;
                    }
                    using (key)
                    {
                        fileName = (string)key.GetValue("EventMessageFile", null);
                    }
                }
                catch (SecurityException)
                {
                    return;
                }
                if (fileName != null)
                {
                    string fileVersion = null;
                    try
                    {
                        fileVersion = FileVersionInfo.GetVersionInfo(fileName).FileVersion;
                    }
                    catch (SecurityException)
                    {
                        return;
                    }
                    catch (FileNotFoundException)
                    {
                        return;
                    }
                    string str4 = Assembly.GetExecutingAssembly().GetName().Version.ToString();
                    ((HttpWebRequest)WebRequest.Create(string.Format(CultureInfo.InvariantCulture, "{0}?EvtSrc={1}&EvtCat=None&EvtID={2}&EvetCatID=0&EvtType={3}&EvtTypeID={4}&CoName={5}&ProdName={6}&ProdVer={7}&FileName={8}&FileVer={9}", new object[] { string.Empty, eventSource, ((uint)eventId) & 0xffff, str, num2, "Microsoft%20Corporation", "Microsoft%c2%ae%20Business%20Solutions%20CRM", str4, Path.GetFileName(fileName), fileVersion }))).BeginGetResponse(new AsyncCallback(ImPMEventLog.RespCallback), null);
                }
            }
        }

        private static void RespCallback(IAsyncResult asynchronousResult)
        {
        }

        public void WriteEntry(EventLogEntryType eventType, long eventId, params object[] values)
        {
            this.WriteEntry(eventType, eventId, true, values);
        }

        public bool WriteEntry(EventLogEntryType eventType, long eventId, bool traceFailure, params object[] values)
        {
            if (this.eventLog == null)
            {
                this.eventLog = new EventLog();
                this.eventLog.Log = "Application";
            }
            bool flag = true;
            bool flag2 = false;
            int categoryId = 0;
            EventInstance instance = new EventInstance(eventId, categoryId, eventType);
            try
            {
                this.eventLog.WriteEvent(instance, values);
            }
            catch (ArgumentException)
            {
                if (this.eventLog.Log != "Application")
                {
                    flag2 = true;
                    this.eventLog.Log = "Application";
                }
            }
            catch (Exception exception)
            {
                if (!flag2 && traceFailure)
                {
                    flag = false;
                    ImPMTrace.TraceFormat(ImPMTrace.DefaultTraceSetting, TraceCategory.Exception, TraceLevel.Error, "Failed to write the following event message to the Event Log.{0}EventType:{1:G}{0}EventId={2}{0}Contents:{3}{0}The exception information is:{0}{4}", new object[] { Environment.NewLine, eventType, eventId, values, exception });
                }
            }
            if (flag2)
            {
                try
                {
                    flag = true;
                    this.eventLog.WriteEvent(instance, values);
                }
                catch (Exception exception2)
                {
                    if (traceFailure)
                    {
                        flag = false;
                        ImPMTrace.TraceFormat(ImPMTrace.DefaultTraceSetting, TraceCategory.Exception, TraceLevel.Error, "Failed to write the following event message to the Event Log.{0}EventType:{1:G}{0}EventId={2}{0}Contents:{3}{0}The exception information is:{0}{4}", new object[] { Environment.NewLine, eventType, eventId, values, exception2 });
                    }
                }
            }
            return flag;
        }

        public void WriteEntry(string eventSourceName, EventLogEntryType eventType, long eventId, params object[] values)
        {
            if (this.eventLog == null)
            {
                this.eventLog = new EventLog();
                this.eventLog.Log = "Application";
            }
            this.eventLog.Source = eventSourceName;
            this.WriteEntry(eventType, eventId, true, values);
        }
    }
}
